package IteratorAndComposite.IteratorInterfacesImpl;

import IteratorAndComposite.entry.MenuItem;

import java.util.Iterator;

/**
 * @Author: pengzx
 * @Description:
 * @Date: 10:54 2017/9/20
 * @Modified by: 1.0
 */
public class DinerMenuJavaIterator implements Iterator {
    MenuItem[] menuItems;
    int position = 0;

    public DinerMenuJavaIterator(MenuItem[] menuItems)
    {
        this.menuItems = menuItems;
    }

    @Override
    public boolean hasNext()
    {
        if (menuItems.length <= position || menuItems[position] == null) {
            return false;
        } else {
            return true;
        }
    }

    @Override
    public Object next()
    {
        MenuItem menuItem = menuItems[position];
        position++;
        return menuItem;
    }

    @Override
    public void remove()
    {
        if (position <= 0) {
            throw new IllegalStateException("无法删除数组元素...");
        }
        if (menuItems[position - 1] != null) {
            for (int i = position-1;i<(menuItems.length-1);i++){
                menuItems[i] = menuItems[i+1];
            }
            menuItems[menuItems.length-1] = null;
        }

    }
}
